package com.ctsi.flowable.idm.remote;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.flowable.engine.service.FlowableService;
import com.ctsi.flowable.idm.dto.GroupDTO;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.idm.api.Group;
import org.flowable.idm.engine.impl.GroupQueryImpl;

import java.util.List;

/**
 * @author: zhangjw
 * @Date: 2018/10/16 10:44
 * @Description: 扩展flowable 用户组查询
 */
public class RemoteGroupQueryImpl extends GroupQueryImpl {
    private static final long serialVersionUID = -7918174095629981265L;

    private final FlowableService organiseServiceProxy;

    public RemoteGroupQueryImpl(FlowableService organiseServiceProxy) {
        this.organiseServiceProxy = organiseServiceProxy;
    }

    @Override
    public long executeCount(CommandContext commandContext) {
      return  this.organiseServiceProxy.getPartysCount(toGroupQuery());
    }

    @Override
    public List<Group> executeList(CommandContext commandContext) {
        List<GroupDTO> groupDTOS = this.organiseServiceProxy.getPartys(toGroupQuery());
        return FlowableIdmUtils.toGroupEntity1(groupDTOS);
    }

    private com.ctsi.flowable.idm.dto.GroupQuery toGroupQuery(){
        com.ctsi.flowable.idm.dto.GroupQuery query = new com.ctsi.flowable.idm.dto.GroupQuery();
        if (UtilValidate.isNotEmpty(getUserId())) {
            query.setUserId(getUserId());
        }
        if (UtilValidate.isNotEmpty(getUserIds())) {
            query.setUserIds(getUserIds());
        }
        if (UtilValidate.isNotEmpty(getIds())) {
            query.setIds(getIds());
        }
        if (UtilValidate.isNotEmpty(getId())) {
            query.setId(getId());
        }
        if (UtilValidate.isNotEmpty(getName())) {
            query.setName(getName());
        }
        if (UtilValidate.isNotEmpty(getNameLike())) {
            query.setNameLike(getNameLike());
        }
        return query;
    }




}
